//
// Created By: Xiaofeng.Gu 2021/04/22
//

#pragma once

#include "../DefinesCore.hpp"
#include <string>
#include <map>
#include <vector>
#include <mutex>

CYBERTRON_BEGIN

class ConfigureClient
{
public:
	ConfigureClient();

	CybertronCoreAPI void Initialize(const std::string& cccIp, const std::string& cccPort);
	CybertronCoreAPI bool InitModule(std::vector<std::string>& modules, std::string& error, std::map<std::string, std::map<std::string, std::string>>& moduleParamValueMap);
	CybertronCoreAPI bool GetConfigure(const std::string& module, std::map<std::string, std::string>& paramValueMap, std::string& error);
	CybertronCoreAPI bool GetValue(const std::string& module, const std::string& param, std::string& value, std::string& error);
	CybertronCoreAPI bool SetCache(const std::string& key, const std::string& value, int ttl, std::string &error);
	CybertronCoreAPI bool GetCache(const std::string& key, std::string& value, std::string &error);


protected:
	bool SetConfigure(std::string& configure, const std::string& module, std::string& error);
	bool SetValue(std::string& valueString, const std::string& module, const std::string& param, std::string& error);
	bool GetUrl(const std::string& url, std::string& result, std::string& error);

protected:
  std::string mCenterServerIp;
  std::string mServerPort;

  std::mutex mMutex;
  std::map<std::string, std::map<std::string, std::string>> mModuleParamValueMap;
};

CybertronCoreAPI extern std::shared_ptr<ConfigureClient> gConfigureClient;
CybertronCoreAPI void createConfigureClient();
CybertronCoreAPI bool setupConfigureClient(
	std::vector<std::string>& modules,
	std::string& error,
	std::map<std::string, std::map<std::string, std::string>>& moduleParamValueMap);
CybertronCoreAPI bool setupConfigureClient(
	const std::string& module,
	std::string& error,
	std::map<std::string, std::string>& paramValueMap);

bool cccGetConfigureFunctionImpl(const std::string& module, std::map<std::string, std::string>& paramValueMap, std::string& error);
bool cccGetValueFunctionImpl(const std::string& module, const std::string& param, std::string& value, std::string& error);
//
#define cccGetConfigureFunction cybertron::cccGetConfigureFunctionImpl
#define cccGetValueFunction cybertron::cccGetValueFunctionImpl

CYBERTRON_END
